<?php
/**
 * Factory class that returns a Alinks_ImportExport_Export_Interface
 * instance
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Factory.php 390 2008-07-03 05:40:51Z sean $
 * @package Alinks_ImportExport
 */
class Alinks_ImportExport_Export_Factory
{
	/**
	 * Returns a new instance of Alinks_ImportExport_Export_Interface
	 *
	 * Given the type specified by $type, this method returns a new
	 * instance of that export class.
	 *
	 * @param string $type The type of export object to create
	 * @param array $options The export class options
	 * @return Alinks_ImportExport_Export_Interface
	 * @throws Alinks_ImportExport_Export_Exception
	 */
	public static function factory($type, $options = array())
	{
		$type = ucwords(strtolower($type));
		$class = 'Alinks_ImportExport_Export_' . $type;
		if (!class_exists($class)) {
			$file = "Alinks/ImportExport/Export/$type.php";
			if (!Alinks_Files::exists($file)) {
				/**
				 * @see Alinks_ImportExport_Export_Exception
				 */
				require_once('Alinks/ImportExport/Export/Exception.php');
				throw new Alinks_ImportExport_Export_Exception("No export class is installed for the type $type. Searched for $file");
			}
			
			require_once($file);
			if (!class_exists($class)) {
				/**
				 * @see Alinks_ImportExport_Export_Exception
				 */
				require_once('Alinks/ImportExport/Export/Exception.php');
				throw new Alinks_ImportExport_Export_Exception("Found $file, but not class $class");
			}
		}
		
		return new $class($options);
	}
}